package com.usc.mock.db.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usc.mock.db.bean.CouponInfo;
import com.usc.mock.db.mapper.CouponInfoMapper;
import com.usc.mock.db.service.CouponInfoService;
import com.usc.mock.utlil.RandomNum;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券表 服务实现类
 * </p>
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {
    @Value("${mock.date}")
    String mockDate;

    public void updateRecentlyDate() {
        List<CouponInfo> couponInfoList = list();

        for (CouponInfo couponInfo : couponInfoList) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            Date stDate = null;
            Date edDate = null;
            try {
                date = simpleDateFormat.parse(mockDate);
                stDate = DateUtils.addDays(date, 0 - (RandomNum.getRandInt(1, 20)));
                edDate = DateUtils.addDays(date, (RandomNum.getRandInt(1, 20)));
            } catch (ParseException e) {
                e.printStackTrace();
            }


            couponInfo.setCreateTime(stDate);
            couponInfo.setStartTime(stDate);
            couponInfo.setExpireTime(edDate);
        }
        saveOrUpdateBatch(couponInfoList);
    }
}
